package com.cms.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.Region;

import com.action.DefaultAction;
import com.cms.manager.CategoryManager;
import com.cms.manager.CpInfoManager;
import com.cms.model.AppChannelApkInfo;
import com.cms.model.AppInfo;
import com.cms.model.AppPointOfBilling;
import com.cms.model.AppPointTerraceInfo;
import com.cms.model.AppScreenshot;
import com.cms.model.AppVersionInfo;
import com.cms.model.AppversionTerraceInfo;
import com.cms.model.Category;
import com.cms.model.Channel;
import com.cms.model.CpInfo;
import com.cms.model.TerraceInfo;
import com.cms.service.AppChannelApkInfoService;
import com.cms.service.AppChannelApkInfoServiceImpl;
import com.cms.service.AppInfoService;
import com.cms.service.AppInfoServiceImpl;
import com.cms.service.AppPointOfBillingService;
import com.cms.service.AppPointOfBillingServiceImpl;
import com.cms.service.AppVersionInfoService;
import com.cms.service.AppVersionInfoServiceImpl;
import com.cms.service.ChannelServiceImpl;
import com.cms.service.TerraceInfoService;
import com.cms.service.TerraceInfoServiceImpl;
import com.util.AnalysisApk;
import com.util.Config;
import com.util.ConfigUtil;
import com.util.FileDigest;
import com.util.Util;


/**   
 *   
 * @author zhanghusong   
 */
public class AppVersionInfoAction extends DefaultAction {

	private static final long serialVersionUID = 1L;
	
	public AppVersionInfoAction()
	{
	}
	AppVersionInfo model=new AppVersionInfo();
	
	String type="";
	String msg="";
	AppVersionInfoService service = new AppVersionInfoServiceImpl();
	AppInfoService appservice = new AppInfoServiceImpl();
	int cpid;
	int categoryparentid;
	int categoryid;
	String appName;
	List<AppVersionInfo> list;
	CpInfo cp;
	List<Category> caplist;
	AppInfo app;
	/**平台列表*/
	private List<TerraceInfo> terraceNameList;
	private TerraceInfoService terraceservice = new TerraceInfoServiceImpl();
	private AppPointOfBillingService appPointService=new AppPointOfBillingServiceImpl();
	private String terraceIds;
	private List<AppversionTerraceInfo> terracelist;	
	private int[] ckb_province;
	/** 渠道平台的关联*/
	private List<AppversionTerraceInfo> appversionTerraceInfos;
	/**计费点列表 */
	private List<AppPointOfBilling> appPointList;
	/**计费点平台的关联*/
	private List<AppPointTerraceInfo> appPointTerraceList;
	/** 计费点的id*/
	private  String apppointids;	
	/** 计费平台的关联*/
	private List<AppPointTerraceInfo> apppointTerraceInfos;
	/**渠道列表*/
	private List<Channel> channelList;
	private String channelids;
	private String[] ckb_apppoint;
	private Map<String,List<AppPointTerraceInfo>> apppointmap;
	
	private Map<String,List<AppVersionInfo>> apvmap;
	private List<AppInfo> applist;
	
	private List<AppScreenshot> aslist;
	private int terraceid;
	
	public String index(){
		apvmap = new LinkedHashMap<String,List<AppVersionInfo>>();
		applist = new AppInfoServiceImpl().getAppInfos();
		
		if(app!=null&&!"".equals(app.getAppnumber())){
			list = service.getAppVersionInfoByAppNumber(app.getAppnumber());
		}else{
			list = service.getAppVersionInfos();
			for (AppInfo item : applist) {
				apvmap.put(item.getAppnumber(), new ArrayList<AppVersionInfo>());
			}
		}
		List<AppVersionInfo> recommendlist = new ArrayList<AppVersionInfo>();
		//取平台名称和版本代码		
		List<AppversionTerraceInfo> l = service.queryAppversionTerrace();//queryAppversionTerraceByAppNumber(app.getAppnumber());
		for (AppVersionInfo appVersion : list) {
			appVersion.setTerraceInfo("");
			for (AppversionTerraceInfo c : l) {
				if (c.getAppversionId()!=appVersion.getId()) continue;
				appVersion.setAppname(appVersion.getAppname()+"("+c.getOperatorsName()+")");
				/*if( c.getRelevanceid()!=null && c.getRelevanceid().length()>0 && c.getGlremark() !=null && c.getGlremark().length()>0){
					if (appVersion.getTerraceInfo().length() > 0 ) {
						appVersion.setTerraceInfo(appVersion.getTerraceInfo() + ",  ");
					}
					appVersion.setTerraceInfo(appVersion.getTerraceInfo() + c.getOperatorsName() + "(" + c.getRelevanceid() + ")");
				}*/
			}
			if(appVersion.getRecommend().equals("1")){
				recommendlist.add(appVersion);
			}else{
				if(!apvmap.containsKey(appVersion.getAppnumber())){
					apvmap.put(appVersion.getAppnumber(), new ArrayList<AppVersionInfo>());
				}
				apvmap.get(appVersion.getAppnumber()).add(appVersion);
			}
		}
		list = recommendlist;
		return SUCCESS;
	}
	public String list()
	{
		app = appservice.getAppInfoByAppNumber(app.getAppnumber());
		cp = CpInfoManager.getInstance().getCpInfo(app.getCpid());
		caplist = CategoryManager.getInstance().getCategorys();
		//版本列表
		list = service.getAppVersionInfoByAppNumber(app.getAppnumber());
		//平台列表
		//取平台名称和版本代码		
		List<AppversionTerraceInfo> l = service.queryAppversionTerraceByAppNumber(app.getAppnumber());
		for (AppVersionInfo appVersion : list) {
			appVersion.setTerraceInfo("");
			for (AppversionTerraceInfo c : l) {
				if (c.getAppversionId()!=appVersion.getId()) continue;
				if( c.getRelevanceid()!=null && c.getRelevanceid().length()>0 /*&& c.getGlremark() !=null && c.getGlremark().length()>0*/){
					if (appVersion.getTerraceInfo().length() > 0 ) {
						appVersion.setTerraceInfo(appVersion.getTerraceInfo() + ",  ");
					}
					appVersion.setTerraceInfo(appVersion.getTerraceInfo() + c.getOperatorsName() + "(" + c.getRelevanceid() + ")");
				}
			}
		}
		//计费点列表
		//appPointList=appPointService.queryAppPoint(app.getAppnumber());
		//得到所有的平台计费点
		/*List<AppPointTerraceInfo> ptList = service.queryAppPointTerraceInfoByAppNumber(app.getAppnumber());		
		//取版本下的所有计费点和计费代码
		for (AppVersionInfo appVersion : list) {
			appVersion.setPointInfo("");
			String terraceName="";
			String pointInfo="";
			for(AppPointTerraceInfo p :ptList){
				if(p.getAppVersionId()!=appVersion.getId()) continue;
				if(p.getPointCode()!=null && p.getPointCode().length()>0){
					if (terraceName.equals("")) {
						terraceName = p.getOperatorsName();
						pointInfo = terraceName + "[";
					}
					else if (!terraceName.equals(p.getOperatorsName())) {
						terraceName = p.getOperatorsName();
						pointInfo = pointInfo.substring(0, pointInfo.length() - 1) + "]," + terraceName + "[";
					}
					pointInfo += p.getPointofbillingname() + "(" + p.getPointCode() + "),";	
				}
			}
			if (!pointInfo.equals("") && pointInfo.length() > 0 && pointInfo != null) {
				pointInfo = pointInfo.substring(0, pointInfo.length() - 1) + "]";
				appVersion.setPointInfo(pointInfo);
			}	
		}*/
		
		return SUCCESS;
	}
	public String showapppoint(){
		List<AppPointTerraceInfo> ptList = service.queryAppPointTerraceInfoByAppNumber(app.getAppnumber());		
		apppointmap = new HashMap<String, List<AppPointTerraceInfo>>();
		//取版本下的所有计费点和计费代码
		for(AppPointTerraceInfo p :ptList){
			if(p.getAppVersionId()!=model.getId()) continue;
			if(p.getPointCode()!=null && p.getPointCode().length()>0){
				if(!apppointmap.containsKey(p.getOperatorsName())){
					apppointmap.put(p.getOperatorsName(), new ArrayList<AppPointTerraceInfo>());
				}
				apppointmap.get(p.getOperatorsName()).add(p);
			}
		}
		/*for (AppVersionInfo appVersion : list) {
			appVersion.setPointInfo("");
			String terraceName="";
			String pointInfo="";
			for(AppPointTerraceInfo p :ptList){
				if(p.getAppVersionId()!=appVersion.getId()) continue;
				if(p.getPointCode()!=null && p.getPointCode().length()>0){
					if (terraceName.equals("")) {
						terraceName = p.getOperatorsName();
						pointInfo = terraceName + "[";
					}
					else if (!terraceName.equals(p.getOperatorsName())) {
						terraceName = p.getOperatorsName();
						pointInfo = pointInfo.substring(0, pointInfo.length() - 1) + "]," + terraceName + "[";
					}
					pointInfo += p.getPointofbillingname() + "(" + p.getPointCode() + "),";	
				}
			}
			if (!pointInfo.equals("") && pointInfo.length() > 0 && pointInfo != null) {
				pointInfo = pointInfo.substring(0, pointInfo.length() - 1) + "]";
				appVersion.setPointInfo(pointInfo);
			}	
		}*/
		return SUCCESS;
	}
	public void doRecommend(){
		service.updateAppVersionRecommend(model);
	}
	/**
	 * 渠道分发
	 * @return
	 */
	public String channellist(){
		model = service.getAppVersionInfoById(model.getId());
		if(type.equals("save")){
			AppChannelApkInfoService appchannelservice = new AppChannelApkInfoServiceImpl();
			AppChannelApkInfo appchannel = new AppChannelApkInfo();
			appchannel.setChannelapknumber(Util.genAppNumber());
			appchannel.setAppnumber(model.getAppnumber());
			appchannel.setAppname(model.getAppname());
			appchannel.setAppversionid(model.getId());
			appchannel.setCpid(model.getCpid());
			appchannel.setCategoryparentid(model.getCategoryparentid());
			appchannel.setCategoryid(model.getCategoryid());
			appchannel.setAppversion(model.getAppversion());
			appchannel.setApppackage(model.getApppackage());
			appchannel.setAppstatus("0");
			appchannel.setAppentry(model.getAppentry());
			appchannel.setCreateuser(getAdminLoginName());
			appchannel.setCreatedate(Util.dateToString(new Date()));
			String[] arr = channelids.split(",");
			for (int i = 0; i < arr.length; i++) {
				String channelid = arr[i];
				if(channelid==null||channelid.equals("")||channelid.length()<=0)continue;
				appchannel.setChannelid(Integer.parseInt(channelid));
				appchannelservice.saveAppChannelApkInfo(appchannel);
			}
		}else{
			channelList = new ChannelServiceImpl().getChannels();
		}
		return SUCCESS;
	}
	/**
	 * 根据平台的id来查找平台的name
	 * @param id
	 * @param l
	 * @return String
	 */
	
	public String terraceIdsToName(String ids, List<TerraceInfo> l) {
		String name = "," + ids + ",";
		for (TerraceInfo t : l) {
			if (name.indexOf("," + t.getId() + ",") != -1) 
				name = name.replace("," + t.getId() + ",", "," + t.getOperatorsName() + ",");
		}
		return name.substring(1, name.length() - 1);
	}
	/**
	 * 根据计费点的id来查找计费点的name
	 * @param ids
	 * @param l
	 * @return String
	 */
	public String billingIdsToName(String ids, List<AppPointOfBilling> l) {
		String name = "," + ids + ",";
		for (AppPointOfBilling t : l) {
			if (name.indexOf("," + t.getId() + ",") != -1) 
				name = name.replace("," + t.getId() + ",", "," + t.getPointofbillingname() + "(" + t.getCode() + ")" + ",");
		}
		return name.substring(1, name.length() - 1);
	}
	
	public String insertTerrace(){
		//平台列表
		terraceNameList = terraceservice.getQuelyTerraceNames();
		terracelist = new ArrayList<AppversionTerraceInfo>();
		for (TerraceInfo item : terraceNameList) {
			if(terraceid==item.getId()){
				AppversionTerraceInfo info = new AppversionTerraceInfo();
				info.setTerraceId(item.getId());
				info.setOperatorsName(item.getOperatorsName());
				terracelist.add(info);
				break;
			}
		}
		//计费点列表
		appPointList=appPointService.queryAppPoint(model.getAppnumber());
		return SUCCESS;
	}
	
	public String show()
	{
		app = appservice.getAppInfoByAppNumber(app.getAppnumber());
		if(type.equals("update"))
		{
			model = service.getAppVersionInfoById(model.getId());
			//if("wl".equals(msg))
				//aslist = appservice.getAppScreenshotByAppVersionNumber(model.getAppversionnumber(),null);
			//平台列表
			terraceNameList = terraceservice.getQuelyTerraceNames();
			// 根据渠道ID查看渠道平台关联信息
			terraceIds = ",";
			terracelist = service.getByAppversionId(model.getId());
			for (AppversionTerraceInfo e : terracelist) {
				terraceIds += e.getTerraceId() + ",";
			}
			appversionTerraceInfos = new ArrayList<AppversionTerraceInfo>();	
			//计费点列表
			appPointList=appPointService.queryAppPoint(app.getAppnumber());
			appPointTerraceList = appPointService.queryAppPointTerrace(model.getId());
			apppointids=",";
			for(AppPointTerraceInfo apt :appPointTerraceList){
				apppointids+=apt.getTerraceId()+"_"+apt.getApppointId() + ",";
			}
			apppointTerraceInfos = new ArrayList<AppPointTerraceInfo>();
		}
		else
		{
			model  = new AppVersionInfo();
			model.setId(-1);
			model.setAppversionnumber(Util.genAppNumber());
			model.setAppnumber(app.getAppnumber());
			model.setAppentry(app.getAppentry());
			model.setAppfilename(app.getAppfilename());
			model.setAppfilesrc(app.getAppfilesrc());
			model.setAppiconname(app.getAppiconname());
			model.setAppiconsrc(app.getAppiconsrc());
			model.setAppmd5(app.getAppmd5());
			model.setAppname(app.getAppname());
			model.setApppackage(app.getApppackage());
			model.setAppremark(app.getAppremark());
			model.setAppversion(app.getAppversion());
			model.setCategoryid(app.getCategoryid());
			model.setCategoryparentid(app.getCategoryparentid());
			model.setCpid(app.getCpid());
			model.setRefilename(app.getRefilename());
			model.setReiconname(app.getReiconname());
			model.setAppstatus("2");
			//平台列表
			terraceNameList = terraceservice.getQuelyTerraceNames();//getQuelyTerraceNamesByAppNumber(app.getAppnumber());
			
			apppointTerraceInfos = new ArrayList<AppPointTerraceInfo>();
			appversionTerraceInfos = new ArrayList<AppversionTerraceInfo>();	
			// 根据平台id分配应用版本平台的ID
			/*terracelist = new ArrayList<AppversionTerraceInfo>();
			for (TerraceInfo terrace : terraceNameList) {
				AppversionTerraceInfo tmp = new AppversionTerraceInfo();
				tmp.setTerraceId(terrace.getId());
				tmp.setId(-1);
				tmp.setOperatorsName(terrace.getOperatorsName());				
				terracelist.add(tmp);
			}
			terraceIds = ",";
			//计费点列表
			appPointList = appPointService.queryAppPoint(app.getAppnumber());
			appPointTerraceList = new ArrayList<AppPointTerraceInfo>();
			//添加所有计费平台中的平台id和计费点id,版本id
			String ptids="";
			for (TerraceInfo terrace : terraceNameList) {
				for(AppPointOfBilling ab:appPointList){
					AppPointTerraceInfo tmp1 = new AppPointTerraceInfo();
					tmp1.setTerraceId(terrace.getId());
					tmp1.setPtId(-1);
					tmp1.setOperatorsName(terrace.getOperatorsName());
					tmp1.setAppVersionId(model.getId());
					tmp1.setApppointId(ab.getId());
					tmp1.setPointCode(ab.getCode());
					appPointTerraceList.add(tmp1);
					ptids=terrace.getId()+"_"+ab.getId()+",";
				}				
				//apppointids = ptids.substring(0, ptids.length()-1)+",";
				apppointTerraceInfos = new ArrayList<AppPointTerraceInfo>();
			} */
			applist = new AppInfoServiceImpl().getAppInfos();
		}
		return SUCCESS;
	}
	
	public String save()	
	{
		if(type.equals("insert"))
		{
			if(service.getAppVersionInfoByAppVersionNumber(model.getAppversionnumber()).size()>0){
				msg="应用版本管理：失败,应用版本ID重复,请检查重新提交。。";
			}else{
				model.setCreateuser(getAdminLoginName());
				model.setCreatedate(Util.dateToString(new Date()));
				model.setAppmd5(model.getRefilename());
				//添加应用版本与平台信息
				List<AppversionTerraceInfo> list = new ArrayList<AppversionTerraceInfo>();
				if (appversionTerraceInfos != null && ckb_province != null) {
					for (AppversionTerraceInfo e : appversionTerraceInfos) {
						for (int n = 0; n < ckb_province.length; n++) {
							if (e.getTerraceId() == ckb_province[n]) {
								list.add(e);
								continue;
							}
						}
					}
				}
				//添加应用计费点与平台信息
				List<AppPointTerraceInfo> ptlist=new ArrayList<AppPointTerraceInfo>();
				if(ckb_province != null && apppointTerraceInfos != null && ckb_apppoint != null){			
					for(AppPointTerraceInfo pt :apppointTerraceInfos){
						for(int n = 0; n < ckb_apppoint.length; n++ ){
							String[] ss = ckb_apppoint[n].split("_");
							int tid = Integer.parseInt(ss[0]);
							int aid = Integer.parseInt(ss[1]);
							for (int i = 0; i < ckb_province.length; i++) {							
								if( tid==ckb_province[i] && pt.getTerraceId()==tid && pt.getApppointId() == aid) {
									ptlist.add(pt);
									continue;
								}
							}
						}
					}
				}
				//保存平台的id
				/*if(null!=ckb_province && ckb_province.length>0){
					String temp = "";
					for(int id : ckb_province){
						temp = temp + id + ",";
					}
					temp = temp.substring(0, temp.length()-1);
					model.setProvinceids(temp);
				}*/
				//添加计费点信息
				/*if(null!=ckb_apppoint && ckb_apppoint.length>0){
					String temp="";
					for(int id : ckb_apppoint){
						temp=temp+id+",";
					}
					temp=temp.substring(0, temp.length()-1);
					model.setApppointids(temp);
				}*/
				if(service.insertAppversionTerraceInfo(model,list,ptlist))
					msg = "平台接入管理：添加成功";
				else
					msg = "平台接入管理：添加失败！请重新添加！";
			}
		}
		else if(type.equals("update"))
		{			
			model.setAppmd5(model.getRefilename());
			//添加应用版本与平台信息
			List<AppversionTerraceInfo> list = new ArrayList<AppversionTerraceInfo>();
			if (appversionTerraceInfos != null && ckb_province != null) {
				for (AppversionTerraceInfo e : appversionTerraceInfos) {
					for (int n = 0; n < ckb_province.length; n++) {
						if (e.getTerraceId() == ckb_province[n]) {
							list.add(e);
							continue;
						}
					}
				}
			}
			//添加应用计费点与平台信息
			List<AppPointTerraceInfo> ptlist=new ArrayList<AppPointTerraceInfo>();
			if(ckb_province != null && apppointTerraceInfos != null && ckb_apppoint != null){			
				for(AppPointTerraceInfo pt :apppointTerraceInfos){
					for(int n = 0; n < ckb_apppoint.length; n++ ){
						String[] ss = ckb_apppoint[n].split("_");
						int tid = Integer.parseInt(ss[0]);
						int aid = Integer.parseInt(ss[1]);
						for (int i = 0; i < ckb_province.length; i++) {							
							if( tid==ckb_province[i] && pt.getTerraceId()==tid && pt.getApppointId() == aid) {
								ptlist.add(pt);
								continue;
							}
						}
						
					}
				}
			}
			//获取应用版本下的平台信息
			/*if(null!=ckb_province && ckb_province.length>0){
				String temp = "";
				for(int id : ckb_province){
					temp = temp + id + ",";
				}
				temp = temp.substring(0, temp.length()-1);
				model.setProvinceids(temp);
			}	*/		
			//添加计费点信息
			/*if(null!=ckb_apppoint && ckb_apppoint.length>0){
				String temp="";
				for(int id : ckb_apppoint){
					temp=temp+id+",";
				}
				temp=temp.substring(0, temp.length()-1);
				model.setApppointids(temp);
			}*/
			if(service.updateAppversionTerraceInfo(model,list,ptlist))
				msg = "平台接入管理：更新成功！";
			else
				msg = "平台接入管理：更新失败！请重新更新！";
			type = "upd";
		}
		else if(type.equals("del"))
		{
			if(service.deleteAppVersionInfo1(model))
				msg = "平台接入管理：删除成功！";
			else
				msg = "平台接入管理：删除失败！请重新删除！";
		}
	
		return SUCCESS;
	}
	private File Filedata;
	private String FiledataFileName;
	private String FiledataContentType;
	private String appscreenshotid;
	private String appscreenshoturl;
	public String saveoffonline()
	{
		model.setAppstatus(model.getAppstatus());
		service.saveAppVersionInfoStatus(model);
		return SUCCESS;
	}
	public String uploadscreenshot()
	{
		if(Filedata!=null)
		{
			String reiconname = FileDigest.getFileMD5(Filedata);
			String filesrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
			"/";
			File f = new File(filesrc);
			if(!f.exists()){
				f.mkdir();
			}
			filesrc+=reiconname;
			saveUploadFile(Filedata,filesrc);
			AppScreenshot as = new AppScreenshot();
			as.setId(-1);
			as.setGenre("1"); //1代表游戏版本信息
			as.setType("2");
			as.setAppnumber(model.getAppnumber());
			as.setAppversionnumber(model.getAppversionnumber());
			as.setImgname(FiledataFileName);
			as.setImgrename(reiconname);
			as.setImgsrc(filesrc);
			appservice.saveAppScreenshot(as);
			aslist = appservice.getAppScreenshotByAppVersionNumber(model.getAppversionnumber(),null);
			appscreenshotid = ""+as.getId();
			appscreenshoturl = reiconname;
		}
		return SUCCESS;
	}
	public String uploadapp()
	{
		if(Filedata!=null)
		{
			String appversionnumber = model.getAppversionnumber();
			String reiconname = FileDigest.getFileMD5(Filedata);
			String filesrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
			"/app/";
			File f = new File(filesrc);
			if(!f.exists()){
				f.mkdir();
			}
			filesrc+=reiconname;
			try {
				String[] st = AnalysisApk.unZip(Filedata, "");
				String packagename = st[1];
				//检查上传文件包名
				if(packagename!=null&&!"".equals(packagename)){
					List<AppVersionInfo> l = service.getAppVersionInfoByAppVersionNumber(appversionnumber);
					if(!packagename.equals(l.get(0).getApppackage())){
						getResponse().setContentType("text/javascript;charset=utf-8");
						PrintWriter pw = getResponse().getWriter();
						pw.write("package");
						pw.flush();
						pw.close();
						return null;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			saveUploadFile(Filedata,filesrc);
			
			AppScreenshot as = new AppScreenshot();
			as.setId(-1);
			as.setGenre("1");
			as.setType("4"); //代表apk
			as.setAppnumber(model.getAppnumber());
			as.setAppversionnumber(appversionnumber);
			as.setImgname(FiledataFileName);
			as.setImgrename(reiconname);
			as.setImgsrc(filesrc);
			appservice.saveAppScreenshot(as);
			appscreenshotid = ""+as.getId();
			appscreenshoturl = reiconname;
			model = new AppVersionInfo();
			model.setRefilename(FiledataFileName);
		}
		return SUCCESS;
	}
	public String uploadicon()
	{
		if(Filedata!=null)
		{
			String reiconname = FileDigest.getFileMD5(Filedata);
			String filesrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
			"/icon/";
			File f = new File(filesrc);
			if(!f.exists()){
				f.mkdir();
			}
			filesrc+=reiconname;
			saveUploadFile(Filedata,filesrc);
			AppScreenshot as = new AppScreenshot();
			as.setId(-1);
			as.setGenre("1");
			as.setType("0");
			as.setAppnumber(model.getAppnumber());
			as.setAppversionnumber(model.getAppversionnumber());
			as.setImgname(FiledataFileName);
			as.setImgrename(reiconname);
			as.setImgsrc(filesrc);
			appservice.saveAppScreenshot(as);
			appscreenshotid = ""+as.getId();
			appscreenshoturl = reiconname;
			
		}
		return SUCCESS;
	}
	public String uploadremark()
	{
		if(Filedata!=null)
		{
			String reiconname = FileDigest.getFileMD5(Filedata);
			String filesrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
			"/remark/";
			File f = new File(filesrc);
			if(!f.exists()){
				f.mkdir();
			}
			filesrc+=reiconname;
			saveUploadFile(Filedata,filesrc);
			AppScreenshot as = new AppScreenshot();
			as.setId(-1);
			as.setGenre("1");
			as.setType("1");
			as.setAppnumber(model.getAppnumber());
			as.setAppversionnumber(model.getAppversionnumber());
			as.setImgname(FiledataFileName);
			as.setImgrename(reiconname);
			as.setImgsrc(filesrc);
			appservice.saveAppScreenshot(as);
			appscreenshotid = ""+as.getId();
			appscreenshoturl = reiconname;
			model = new AppVersionInfo();
			model.setRefilename(FiledataFileName);
		}
		return SUCCESS;
	}
	public String uploadremark1()
	{
		if(Filedata!=null)
		{
			String reiconname = FileDigest.getFileMD5(Filedata);
			String filesrc = ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
			"/remark1/";
			File f = new File(filesrc);
			if(!f.exists()){
				f.mkdir();
			}
			filesrc+=reiconname;
			saveUploadFile(Filedata,filesrc);
			AppScreenshot as = new AppScreenshot();
			as.setId(-1);
			as.setGenre("1");
			as.setType("3");
			as.setAppnumber(model.getAppnumber());
			as.setAppversionnumber(model.getAppversionnumber());
			as.setImgname(FiledataFileName);
			as.setImgrename(reiconname);
			as.setImgsrc(filesrc);
			appservice.saveAppScreenshot(as);
			appscreenshotid = ""+as.getId();
			appscreenshoturl = reiconname;
			model = new AppVersionInfo();
			model.setRefilename(FiledataFileName);
		}
		return SUCCESS;
	}
	public String d()
	{
		try {
			HttpServletResponse response = super.getResponse();
			InputStream is = new BufferedInputStream(new FileInputStream(ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_CP_APP_SAVE_PATH) + 
					"/"+appscreenshoturl));
			byte[] b = new byte[is.available()];
			is.read(b);
			is.close();
			
			String fileName = "1111";
	    	response.setContentType("application/octet-stream");
	    	response.setHeader("Location", fileName);
	    	response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
	    	OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
	    	outputStream.write(b);
	    	outputStream.flush();
	    	outputStream.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	//下载apk
	public  void downApp(){
		AppVersionInfo info=null;
		
		if(model.getId()>0){
			 info= service.getAppVersionInfoById(model.getId());
		}		
		String path=info.getAppfilesrc();//文件路径
		if(!path.equals("")){
			// path是指欲下载的文件的路径。
	        File file = new File(path);
	        // 取得文件名。
	        String filename = info.getAppfilename();//file.getName();
	        // 取得文件的后缀名。
	        String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
	        try{
		        // 以流的形式下载文件。
		        InputStream fis = new BufferedInputStream(new FileInputStream(path));
		        byte[] buffer = new byte[fis.available()];
		        fis.read(buffer);
		        fis.close();
		        
		        // 清空response
		        this.getResponse().reset();
		        // 设置response的Header
		        this.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
		        this.getResponse().addHeader("Content-Length", "" + file.length());
		        OutputStream toClient = new BufferedOutputStream(this.getResponse().getOutputStream());
		        this.getResponse().setContentType("application/octet-stream");
		        toClient.write(buffer);
		        toClient.flush();
		        toClient.close();
		    } catch (IOException ex) {
		    	ex.printStackTrace();
		    }
			}
		else{
			msg = "您还没有上传apk,请上传aqk";
		}
	}

	public String exportFile(){
		showapppoint();
		if(apppointmap!=null){
			// 获取总列数
			int CountColumnNum = 8;
			// 创建Excel文档
			HSSFWorkbook hwb = new HSSFWorkbook();
			HSSFCellStyle style = hwb.createCellStyle();   //样式对象  
		    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直     
		    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平  
			AppPointTerraceInfo xlsDto = null;
			// sheet 对应一个工作页
			HSSFSheet sheet = hwb.createSheet("point");
			HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
			HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
			String[] names = new String[CountColumnNum];
			names[0] = "平台名称";
			names[1] = "计费点代码";
			names[2] = "计费点名称";
			names[3] = "资费(分)";
			names[4] = "类型";
			names[5] = "触发计费说明";
			names[6] = "道具功能";
			names[7] = "描述";
			for (int j = 0; j < CountColumnNum; j++) {
				firstcell[j] = firstrow.createCell(j);
				firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
			}
			int startrow = 1,endrow=0,k=0;
			for (Entry<String,List<AppPointTerraceInfo>> item : apppointmap.entrySet()) {
				String key = item.getKey();
				List<AppPointTerraceInfo> list = item.getValue();
				endrow+=list.size();
				sheet.addMergedRegion(new Region(startrow,(short)0,endrow,(short)0));
				startrow+=list.size();
				for (int i = 0; i < list.size(); i++) {
					// 创建一行
					k+=1;
					HSSFRow row = sheet.createRow(k);
					xlsDto = list.get(i);
					if(i==0){
						HSSFCell c0 = row.createCell(0);
						c0.setCellValue(key);
						c0.setCellStyle(style);
					}
					HSSFCell c1 = row.createCell(1);
					c1.setCellValue(xlsDto.getPointCode());
					HSSFCell c2 = row.createCell(2);
					c2.setCellValue(xlsDto.getPointofbillingname());
					HSSFCell c3 = row.createCell(3);
					c3.setCellValue(xlsDto.getFee());
					HSSFCell c4 = row.createCell(4);
					String type="";
					if("0".equals(xlsDto.getCodetype())){
						type="单次";
					}else if("1".equals(xlsDto.getCodetype())){
						type = "重复";
					}
					c4.setCellValue(type);
					HSSFCell c5 = row.createCell(5);
					c5.setCellValue(xlsDto.getCodetriggerremark());
					HSSFCell c6 = row.createCell(6);
					c6.setCellValue(xlsDto.getCodepropremark());
					HSSFCell c7 = row.createCell(7);
					c7.setCellValue(xlsDto.getCoderemark());
					
				}
		}
		// 创建文件输出流，准备输出电子表格
			String filename = "point.xls";
		try {
			// 清空response
	        this.getResponse().reset();
	        // 设置response的Header
	        this.getResponse().addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
	       // this.getResponse().addHeader("Content-Length", "" + file.length());
	        OutputStream toClient = new BufferedOutputStream(this.getResponse().getOutputStream());
	        this.getResponse().setContentType("application/octet-stream");
	        hwb.write(toClient);
	        toClient.flush();
	        toClient.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		}else{
			System.out.println("apppointmap is null");
		}
		return null;
	}
	public String chkTerrace(){
		List<AppversionTerraceInfo> apvtlist = service.queryApvtByAppnumberAndTerraceId(model.getAppnumber(),terraceIds);
		String flag="ok";
		if(apvtlist.size()>0){
			flag="false";
		}
		try {
			PrintWriter pw = getResponse().getWriter();
			pw.write(flag);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String getAppscreenshotid() {
		return appscreenshotid;
	}
	public void setAppscreenshotid(String appscreenshotid) {
		this.appscreenshotid = appscreenshotid;
	}
	public String getAppscreenshoturl() {
		return appscreenshoturl;
	}
	public void setAppscreenshoturl(String appscreenshoturl) {
		this.appscreenshoturl = appscreenshoturl;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getCpid() {
		return cpid;
	}
	public void setCpid(int cpid) {
		this.cpid = cpid;
	}
	public int getCategoryparentid() {
		return categoryparentid;
	}
	public void setCategoryparentid(int categoryparentid) {
		this.categoryparentid = categoryparentid;
	}
	public int getCategoryid() {
		return categoryid;
	}
	public void setCategoryid(int categoryid) {
		this.categoryid = categoryid;
	}
	public String getAppName() {
		return appName;
	}
	public void setAppName(String appName) {
		this.appName = appName;
	}
	public File getFiledata() {
		return Filedata;
	}
	public void setFiledata(File filedata) {
		Filedata = filedata;
	}
	public String getFiledataFileName() {
		return FiledataFileName;
	}
	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}
	public String getFiledataContentType() {
		return FiledataContentType;
	}
	public void setFiledataContentType(String filedataContentType) {
		FiledataContentType = filedataContentType;
	}
	public AppVersionInfo getModel() {
		return model;
	}
	public void setModel(AppVersionInfo model) {
		this.model = model;
	}
	public List<AppVersionInfo> getList() {
		return list;
	}
	public void setList(List<AppVersionInfo> list) {
		this.list = list;
	}
	public AppInfo getApp() {
		return app;
	}
	public void setApp(AppInfo app) {
		this.app = app;
	}
	public CpInfo getCp() {
		return cp;
	}
	public void setCp(CpInfo cp) {
		this.cp = cp;
	}
	public List<Category> getCaplist() {
		return caplist;
	}
	public void setCaplist(List<Category> caplist) {
		this.caplist = caplist;
	}
	
	public List<TerraceInfo> getTerraceNameList() {
		return terraceNameList;
	}
	public void setTerraceNameList(List<TerraceInfo> terraceNameList) {
		this.terraceNameList = terraceNameList;
	}
	public TerraceInfoService getTerraceservice() {
		return terraceservice;
	}
	public void setTerraceservice(TerraceInfoService terraceservice) {
		this.terraceservice = terraceservice;
	}
	public String getTerraceIds() {
		return terraceIds;
	}
	public void setTerraceIds(String terraceIds) {
		this.terraceIds = terraceIds;
	}
	public List<AppversionTerraceInfo> getTerracelist() {
		return terracelist;
	}
	public void setTerracelist(List<AppversionTerraceInfo> terracelist) {
		this.terracelist = terracelist;
	}
	
	public int[] getCkb_province() {
		return ckb_province;
	}
	public void setCkb_province(int[] ckb_province) {
		this.ckb_province = ckb_province;
	}
	public List<AppPointOfBilling> getAppPointList() {
		return appPointList;
	}
	public void setAppPointList(List<AppPointOfBilling> appPointList) {
		this.appPointList = appPointList;
	}
	
	public String[] getCkb_apppoint() {
		return ckb_apppoint;
	}
	public void setCkb_apppoint(String[] ckb_apppoint) {
		this.ckb_apppoint = ckb_apppoint;
	}

	public List<AppversionTerraceInfo> getAppversionTerraceInfos() {
		return appversionTerraceInfos;
	}

	public void setAppversionTerraceInfos(
			List<AppversionTerraceInfo> appversionTerraceInfos) {
		this.appversionTerraceInfos = appversionTerraceInfos;
	}

	public List<AppPointTerraceInfo> getAppPointTerraceList() {
		return appPointTerraceList;
	}

	public void setAppPointTerraceList(List<AppPointTerraceInfo> appPointTerraceList) {
		this.appPointTerraceList = appPointTerraceList;
	}

	public List<AppPointTerraceInfo> getApppointTerraceInfos() {
		return apppointTerraceInfos;
	}

	public void setApppointTerraceInfos(
			List<AppPointTerraceInfo> apppointTerraceInfos) {
		this.apppointTerraceInfos = apppointTerraceInfos;
	}
	
	public String getApppointids() {
		return apppointids;
	}

	public void setApppointids(String apppointids) {
		this.apppointids = apppointids;
	}
	public List<Channel> getChannelList() {
		return channelList;
	}
	public void setChannelList(List<Channel> channelList) {
		this.channelList = channelList;
	}
	public String getChannelids() {
		return channelids;
	}
	public void setChannelids(String channelids) {
		this.channelids = channelids;
	}
	public Map<String, List<AppPointTerraceInfo>> getApppointmap() {
		return apppointmap;
	}
	public void setApppointmap(Map<String, List<AppPointTerraceInfo>> apppointmap) {
		this.apppointmap = apppointmap;
	}
	public Map<String, List<AppVersionInfo>> getApvmap() {
		return apvmap;
	}
	public void setApvmap(Map<String, List<AppVersionInfo>> apvmap) {
		this.apvmap = apvmap;
	}
	public List<AppInfo> getApplist() {
		return applist;
	}
	public void setApplist(List<AppInfo> applist) {
		this.applist = applist;
	}
	public List<AppScreenshot> getAslist() {
		return aslist;
	}
	public void setAslist(List<AppScreenshot> aslist) {
		this.aslist = aslist;
	}
	public int getTerraceid() {
		return terraceid;
	}
	public void setTerraceid(int terraceid) {
		this.terraceid = terraceid;
	}
	

}
